/**
 * navbar.js
 * @author 御风 <1945199284@qq.com>
 */
layui.define(['element', 'configureCom', 'sessionFun', 'configure'], function (exports) {
    var $ = layui.jquery,
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        element = layui.element,
        common = layui.common,
        cacheName = 'tb_navbar';
    var sessionFun = layui.sessionFun;
    var configureCom = layui.configureCom;
    var configure = layui.configure;

    var Navbar = function () {
        /**
         *  默认配置
         */
        this.config = {
            elem: undefined, //容器
            data: undefined, //数据源
            url: undefined, //数据源地址
            type: 'GET', //读取方式
            cached: false, //是否使用缓存
            spreadOne: false //设置是否只展开一个二级菜单
        };
        this.v = '1.0.0';
    };
    //渲染
    Navbar.prototype.render = function () {
        var _that = this;
        var _config = _that.config;
        if (typeof (_config.elem) !== 'string' && typeof (_config.elem) !== 'object') {
            configureCom.throwError('Navbar error: elem参数未定义或设置出错，具体设置格式请参考文档API.');
        }
        var $container;
        if (typeof (_config.elem) === 'string') {
            $container = $('' + _config.elem + '');
        }
        if (typeof (_config.elem) === 'object') {
            $container = _config.elem;
        }
        if ($container.length === 0) {
            configureCom.throwError('Navbar error:找不到elem参数配置的容器，请检查.');
        }
        if (_config.data === undefined && _config.url === undefined) {
            configureCom.throwError('Navbar error:请为Navbar配置数据源.')
        }
        if (_config.data !== undefined && typeof (_config.data) === 'object') {
            var html = getHtml(_config.data);
            $container.html(html);
            element.init();
            _that.config.elem = $container;
        } else {
            if (_config.cached) {
                var cacheNavbar = layui.data(cacheName);
                if (cacheNavbar.navbar === undefined) {
                    configureCom.getAjax(_config.url).then(function (res) {
                        sessionFun.set(configure.SESSION_LOGIN_INFOR_DATA, res);
                        layui.data(cacheName, {
                            key: 'navbar',
                            value: res.menuList
                        });
                        var html = getHtml(res.menuList);
                        $container.html(html);
                        element.init();
                        _that.config.elem = $container;
                    });
                } else {
                    var html = getHtml(cacheNavbar.navbar);
                    $container.html(html);
                    element.init();
                    _that.config.elem = $container;
                }
            } else {
                //清空缓存
                layui.data(cacheName, null);
                configureCom.getAjax(_config.url).then(function (res) {
                    console.log(res)
                    sessionFun.set(configure.SESSION_LOGIN_INFOR_DATA, res);
                    sessionFun.set(configure.SESSION_LOGIN_ORG_DATA, res.result.org);
                    sessionFun.set(configure.SESSION_LOGIN_USER_DATA, res.result.user);
                    var html = getHtml(res.result.menuList);
                    $container.html(html);
                    element.init();
                    _that.config.elem = $container;
                });
            }
        }

        //只展开一个二级菜单
        if (_config.spreadOne) {
            var $ul = $container.children('ul');
            $ul.find('li.layui-nav-item').each(function () {
                $(this).on('click', function () {
                    $(this).siblings().removeClass('layui-nav-itemed');
                });
            });
        }
        return _that;
    };
    /**
     * 配置Navbar
     * @param {Object} options
     */
    Navbar.prototype.set = function (options) {
        var that = this;
        that.config.data = undefined;
        $.extend(true, that.config, options);
        return that;
    };
    /**
     * 绑定事件
     * @param {String} events
     * @param {Function} callback
     */
    Navbar.prototype.on = function (events, callback) {
        var that = this;
        var _con = that.config.elem;
        if (typeof (events) !== 'string') {
            configureCom.throwError('Navbar error:事件名配置出错，请参考API文档.');
        }
        var lIndex = events.indexOf('(');
        var eventName = events.substr(0, lIndex);
        var filter = events.substring(lIndex + 1, events.indexOf(')'));
        if (eventName === 'click') {
            if (_con.attr('lay-filter') !== undefined) {
                _con.children('ul').find('li').each(function () {
                    var $this = $(this);
                    if ($this.find('dl').length > 0) {
                        var $dd = $this.find('dd').each(function () {
                            $(this).on('click', function () {
                                var $a = $(this).children('a');
                                var href = $a.data('url');
                                var icon = $a.children('i:first').data('icon');
                                var title = $a.children('cite').text();
                                var data = {
                                    elem: $a,
                                    field: {
                                        href: href,
                                        icon: icon,
                                        title: title
                                    }
                                };
                                callback(data);
                            });
                        });
                    } else {
                        $this.on('click', function () {
                            var $a = $this.children('a');
                            var href = $a.data('url');
                            var icon = $a.children('i:first').data('icon');
                            var title = $a.children('cite').text();
                            var data = {
                                elem: $a,
                                field: {
                                    href: href,
                                    icon: icon,
                                    title: title
                                }
                            };
                            callback(data);
                        });
                    }
                });
            }
        }
    };
    /**
     * 清除缓存
     */
    Navbar.prototype.cleanCached = function () {
        layui.data(cacheName, null);
    };

    /**
     * 获取html字符串
     * @param {Object} data
     */
    function getHtml(data) {
        var ulHtml = '<ul class="layui-nav layui-nav-tree beg-navbar">';
        for (var i = 0; i < data.length; i++) {
            if (data[i].spread) {
                ulHtml += '<li class="layui-nav-item layui-nav-itemed">';
            } else {
                ulHtml += '<li class="layui-nav-item">';
            }
            if (data[i].children !== undefined && data[i].children !== null && data[i].children.length > 0) {
                ulHtml += '<a href="javascript:;">';
                ulHtml += '<span class="layui-nav-more"></span>';
                data[i].icon = decodeURIComponent(data[i].icon);
                ulHtml += '<i class="layui-icon" data-icon="' + data[i].icon + '">' + data[i].icon + '</i>';
                ulHtml += '<cite>' + data[i].name + '</cite>';
                ulHtml += '</a>';
                ulHtml += '<dl class="layui-nav-child">';
                //二级菜单
                for (var j = 0; j < data[i].children.length; j++) {
                    data[i].children[j].icon = decodeURIComponent(data[i].children[j].icon);
                    //是否有孙子节点
                    if (data[i].children[j].children !== undefined && data[i].children[j].children !== null && data[i].children[j].children.length > 0) {
                        ulHtml += '<dd>';
                        ulHtml += '<a href="javascript:;">';
                        ulHtml += '<span class="layui-nav-more"></span>';
                        ulHtml += '<i class="layui-icon" data-icon="' + data[i].children[j].icon + '">' + data[i].children[j].icon + '</i>';
                        ulHtml += '<cite>' + data[i].children[j].name + '</cite>';
                        ulHtml += '</a>';
                        //三级菜单
                        ulHtml += '<dl class="layui-nav-child">';
                        var grandsonNodes = data[i].children[j].children;
                        for (var k = 0; k < grandsonNodes.length; k++) {
                            grandsonNodes[k].icon = decodeURIComponent(grandsonNodes[k].icon);
                            ulHtml += '<dd>';
                            ulHtml += '<a data-url="' + grandsonNodes[k].url + '" data-id="' + grandsonNodes[k].menuId + '" style="padding-left:60px">';
                            ulHtml += '<i class="layui-icon" data-icon="' + grandsonNodes[k].icon + '">' + grandsonNodes[k].icon + '</i>';
                            ulHtml += '<cite>' + grandsonNodes[k].name + '</cite></a>';
                            ulHtml += '</dd>';
                        }
                        ulHtml += '</dl>';
                        ulHtml += '</dd>';
                    } else {
                        if(data[i].children[j].name === '待办项目' || data[i].children[j].name === '待审项目'|| data[i].children[j].name === '在线报价' ){
                            ulHtml += '<dd>';
                            ulHtml += '<a data-url="' + data[i].children[j].url + '" data-id="' + data[i].children[j].menuId + '">';
                            ulHtml += '<i class="layui-icon" data-icon="' + data[i].children[j].icon + '">' + data[i].children[j].icon + '</i>';
                            ulHtml += '<cite>' + data[i].children[j].name + '</cite>';
                            ulHtml += '<span class="layui-badge">' +  + '</span>';
                            ulHtml += '</a>';
                            ulHtml += '</dd>';
                        }else{
                            ulHtml += '<dd>';
                            ulHtml += '<a data-url="' + data[i].children[j].url + '" data-id="' + data[i].children[j].menuId + '">';
                            ulHtml += '<i class="layui-icon" data-icon="' + data[i].children[j].icon + '">' + data[i].children[j].icon + '</i>';
                            ulHtml += '<cite>' + data[i].children[j].name + '</cite>';
                            ulHtml += '</a>';
                            ulHtml += '</dd>';
                        }
                    }
                    //ulHtml += '<dd title="' + data[i].children[j].title + '">'; 
                }
                ulHtml += '</dl>';
            } else {
                var dataUrl = (data[i].url !== undefined && data[i].url !== '') ? 'data-url="' + data[i].url + '"' : '';
                //ulHtml += '<a data-url"javascript:;" ' + dataUrl + '>';
                ulHtml += '<a data-url="' + data[i].url + '" data-id="' + data[i].menuId + '">';
                if (data[i].icon !== undefined && data[i].icon !== '') {
                    data[i].icon = decodeURIComponent(data[i].icon);
                    ulHtml += '<i class="layui-icon" data-icon="' + data[i].icon + '">' + data[i].icon + '</i>';
                }
                ulHtml += '<cite>' + data[i].name + '</cite>';
                ulHtml += '</a>';
            }
            ulHtml += '</li>';
        }
        ulHtml += '</ul>';

        //获取待办数量
        configureCom.postDataAjax(configure.PROC_WAIT_DEAL_NUM_API).then(function(res){
            console.log(res)
            var wait =  res.toDo;
            $('.layui-badge').html(wait)
        });
        return ulHtml;
    }

    var navbar = new Navbar();

    exports('navBar', function (options) {
        return navbar.set(options);
    });
});